package com.sundae.smarthomedemo.Widget;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.StyleRes;
import android.view.View;
import android.view.Window;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.ListView;
import android.widget.SimpleAdapter;

import com.sundae.smarthomedemo.R;

import java.util.ArrayList;
import java.util.HashMap;


/**
 * Created by daijiyuan on 2017/6/6.
 * 邮箱 948820549@qq.com
 *
 * @Sundae
 */

public class DeviceDialog extends Dialog implements View.OnClickListener,OnItemClickListener {

    private Context context;

    private ListView deviceListView = null;

    private Button canelButton = null;

    private CallBack callBack = null;

    private ArrayList<HashMap<String , String>> deviceInfo = new ArrayList<>();
    private SimpleAdapter simpleAdapter;

    public DeviceDialog(@NonNull Context context) {
        super(context);
        this.context = context;
    }

    public DeviceDialog(@NonNull Context context, @StyleRes int themeResId) {
        super(context, themeResId);
        this.context = context;
    }

    public DeviceDialog(@NonNull Context context, @StyleRes int themeResId , ArrayList<HashMap<String , String>> deviceInfoList) {
        super(context, themeResId);
        this.context = context;
        this.deviceInfo = deviceInfoList;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Window win = getWindow();
        win.requestFeature(Window.FEATURE_NO_TITLE);
        this.setContentView(R.layout.device_scan_dialog);

        init();
    }

    private void init()
    {

        deviceListView = (ListView) findViewById(R.id.lvDevice);
        canelButton = (Button) findViewById(R.id.btnCancelScan);

        canelButton.setOnClickListener(this);
        deviceListView.setOnItemClickListener(this);

        simpleAdapter = new SimpleAdapter(context ,
                deviceInfo ,
                R.layout.device_list_item ,
                new String[]{"ssid" , "haspwd"} ,
                new int[]{R.id.tv_ssid , R.id.tv_haspwd}
        );
        deviceListView.setAdapter(simpleAdapter);

        deviceListView.setSelection(0);
        deviceListView.smoothScrollToPosition(0);
    }

    public void setCallBack(CallBack callBack)
    {
        this.callBack = callBack;
    }

    @Override
    public void onClick(View v) {
        dismiss();
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        if(callBack != null) callBack.onDeviceSelected(deviceInfo.get(position).get("ssid") , deviceInfo.get(position).get("haspwd").equals("有加密"));
    }

    @Override
    public void dismiss() {
        super.dismiss();
    }

    public interface CallBack
    {
        void onDeviceSelected(String ssid, boolean hasPwd);
    }
}
